package cn.edu.seig.textbook.controller;

import cn.edu.seig.textbook.domain.pojo.Students;
import cn.edu.seig.textbook.domain.pojo.Teachers;
import cn.edu.seig.textbook.domain.pojo.Textbooks;
import cn.edu.seig.textbook.domain.pojo.Users;

import cn.edu.seig.textbook.enums.SexEnum;
import cn.edu.seig.textbook.service.IStudentsService;
import cn.edu.seig.textbook.service.ITeachersService;
import cn.edu.seig.textbook.service.ITextbooksService;
import cn.edu.seig.textbook.service.IUsersService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


/**
 * @author lenovo yjh
 */
@Controller
@RequestMapping("/admin")
public class adminController {
    @Autowired
    ITeachersService teachersService;
    @Autowired
    private ITextbooksService textbooksService;
    @Autowired
    private IUsersService usersService;
    @Autowired
    private IStudentsService studentsService;

    //查询所有学生
    @RequestMapping("/student_all")
    public String studentAll(HttpServletRequest request){
        List<Students> students = studentsService.getStudents();
        request.setAttribute("students", students);
        return "admin/showAllStudent";

    }
    @RequestMapping("/student_add")
    public String studentAdd(){
        return "admin/addStudent";
    }

    @RequestMapping("/student_save")
    public String studentSave(HttpServletRequest request,Students student, Users user,@Param("a") int a){
        if(usersService.findByUserId(user.getUserId())!=null){
            request.setAttribute("msg","用户ID重复，请重新输入");
            return "admin/addStudent";
        }
        user.setPermissionId(1);
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));

        student.setGender(SexEnum.fromSex(a));
        if(usersService.addUsers(user)&studentsService.addStudent(student)) {
            System.out.println("学生添加成功！");
        }
        else {
            System.out.println("添加学生失败！");
        }

        return "redirect:/admin/student_all";
    }

    //根据id查询教师信息返回更改页面
    @RequestMapping("/findStudentById")
    public String findStudentById(@RequestParam("studentId") String studentId,Model model) {
        Students student=studentsService.getStudentById(studentId);
        Users user= usersService.findByUserId(student.getUserId());

        model.addAttribute("user", user);
        model.addAttribute("student", student);

        return "admin/updateStudent";
    }
    @RequestMapping("/student_update")
    public String studentUpdate(Students student, Users user,@Param("a") int a){
        user.setPermissionId(1);
        student.setGender(SexEnum.fromSex(a));
        if(usersService.addUsers(user)&studentsService.addStudent(student)) {
            System.out.println("学生添加成功！");
        }
        else {
            System.out.println("添加学生失败！");
        }

        return "redirect:/admin/student_all";
    }

    @RequestMapping("/deleteStudent")
    public String deleteStudent(@Param("studentId") String studentId){
        Students student=studentsService.getStudentById(studentId);
        studentsService.deleteStudentById(studentId);
        usersService.deleteByUserId(student.getUserId());
        return "redirect:/admin/student_all";

    }

    //批量删除学生
    @RequestMapping("/deleteStudents")
    public String studentsDeleteAll(@RequestParam("ids") String[] ids){
        System.out.println(ids.length);

        for(String id:ids){
            Students student=studentsService.getStudentById(id);
            studentsService.deleteStudentById(id);
            usersService.deleteByUserId(student.getUserId());
        }
        return "redirect:/admin/student_all";
    }

    @RequestMapping("/textbook_all")
    public String textbookAll(HttpServletRequest request) {
        List<Textbooks> textbooks=textbooksService.getAllTestbooks();
        request.setAttribute("textbooks", textbooks);
        return "admin/showAllTextbook";
    }
    //查询所有教师
    @RequestMapping("/teacher_all")
    public String teacherAll(HttpServletRequest request){
        List<Teachers> teachers = teachersService.findAll();

        request.setAttribute("teachers", teachers);
        return "admin/showAllTeacher";

    }

    //跳转添加教师页面
    @RequestMapping("/teacher_add")
    public String teacherAdd(){
        return "admin/addTeacher";
    }

    //添加教师
    @RequestMapping("/teacher_save")
    public String teacherSave(HttpServletRequest request,Teachers teacher, Users user){
        if(usersService.findByUserId(user.getUserId())!=null){
            request.setAttribute("msg","用户ID重复，请重新输入");
            return "admin/addTeacher";
        }
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        if(usersService.addUsers(user)&teachersService.add(teacher)) {
            System.out.println("教师添加成功！");
        }
        else {
            System.out.println("添加教师失败！");
        }

        return "redirect:/admin/teacher_all";
    }




    //根据id查询教师信息返回更改页面
    @RequestMapping("/findTeacherById")
    public String findTeacherById(@RequestParam("teacherId") int teacherId,Model model) {
        Teachers teacher=teachersService.findById(teacherId);
        Users user= usersService.findByUserId(teacher.getUserId());
        model.addAttribute("user", user);
        model.addAttribute("teacher", teacher);
        return "admin/updateTeacher";
    }


    //修改教师及账户
    @RequestMapping("teacher_update")
    public String updateTeacher(Teachers teacher, Users user){

        if(usersService.addUsers(user)&teachersService.add(teacher)) {
            System.out.println("教师修改成功！");
        }
        else {
            System.out.println("修改教师失败！");
        }

        return "redirect:/admin/teacher_all";
    }

    //删除教师及账户
    @RequestMapping("/deleteTeacher")
    public String deleteTeacher(@RequestParam("teacherId") int teacherId){
        Teachers teacher=teachersService.findById(teacherId);
        teachersService.delete(teacherId);
        usersService.deleteByUserId(teacher.getUserId());
        return "redirect:/admin/teacher_all";
    }

    //根据id查询教材信息返回更改页面
    @RequestMapping("/findTextbookById")
    public String findTextbookById(@RequestParam("textbookId") int textbookId,Model model){
        Textbooks textbook=textbooksService.getTextbookById(textbookId);
        model.addAttribute("textbook", textbook);
        return "admin/updateTextbook";
    }
    //跳转添加教材页面
    @RequestMapping("/textbook_add")
    public String textbookAdd(){
        return "admin/addTextbook";
    }

    //添加教材
    @RequestMapping("/textbook_save")
    public String textbookSave(HttpServletRequest request,Textbooks textbook){
        if (textbooksService.getTextbookById(textbook.getTextbookId())!=null){
            request.setAttribute("msg","教材ID重复，请重新输入");
            return "admin/addTextbook";
        }
        if(textbooksService.addTextbook(textbook)) {
            System.out.println("教材添加成功！");
        }
        else {
            System.out.println("专业教材失败！");
        }
        return "redirect:/admin/textbook_all";
    }
    //修改教材信息
    @RequestMapping("/textbook_update")
    public String textbookUpdate(Textbooks textbook){
        if(textbooksService.addTextbook(textbook)) {
            System.out.println("教材添加成功！");
        }
        else {
            System.out.println("专业教材失败！");
        }
        return "redirect:/admin/textbook_all";
    }
    //批量删除教材
    @RequestMapping("/deleteTeachers")
    public String deleteTeachers(@RequestParam("ids") int[] ids){
        System.out.println(ids.length);

        for(int id:ids){
            Teachers teacher=teachersService.findById(id);
            teachersService.delete(id);
            usersService.deleteByUserId(teacher.getUserId());
        }

        return "redirect:/admin/teacher_all";
    }

    //删除教材
    @RequestMapping("/deleteTextbook")
    public String deleteTextbook(@RequestParam("textbookId") int textbookId){
        textbooksService.deleteTextbook(textbookId);
        return "redirect:/admin/textbook_all";
    }

    //批量删除教材
    @RequestMapping("/deleteTextbooks")
    public String deleteTextbooks(@RequestParam("ids") int[] ids){
        System.out.println(ids.length);

        for(int id:ids){
            textbooksService.deleteTextbook(id);
        }
        return "redirect:/admin/textbook_all";
    }


    //管理员首页
    @RequestMapping("/toAdmin")
    public String admin(Model model, @AuthenticationPrincipal UserDetails userDetails, HttpSession session) {
        session.setAttribute("userDetails", userDetails);
        return "admin/admin";
    }

}
